package com.ebeitech.auction.domain.dao;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;

import com.ebeitech.auction.application.EbeiApplication;
import com.ebeitech.auction.db.CommonDBHelper;
import com.ebeitech.auction.db.DBChangeInterface;
import com.ebeitech.auction.db.dao.BaseDBDaoImpl;
import com.ebeitech.auction.domain.ChatMessage;
import com.ebeitech.library.constant.MessageConstants;
import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.util.CommonUtil;
import com.ebeitech.library.util.SPManager;
import com.ebeitech.library.util.StringUtil;
import com.ebeitech.library.util.TimeUtil;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by alice on 2017/11/13.
 */
public class ChatMessageDao extends BaseDBDaoImpl<ChatMessage> implements DBChangeInterface {

	private static ChatMessageDao chatMessageDao;

	public static ChatMessageDao getInstance() {

		if(chatMessageDao == null || chatMessageDao.dbHelper == null){
			chatMessageDao = new ChatMessageDao();
		}
		return chatMessageDao;
	}

	private ChatMessageDao() {
		super(CommonDBHelper.getDBHelper(), ChatMessage.class);
		CommonDBHelper.addToDaoManager(this);
	}

	@Override
	public void DBChange() {
		chatMessageDao = null;
	}

	public void handlerMessage(String message) {
		Context context = EbeiApplication.getContext();
		ChatMessage chatMessage = new Gson().fromJson(message, ChatMessage.class);
		boolean isSendFromSelf = false;
		if (chatMessage != null) {
			chatMessage.setMessageState(ChatMessage.MESSAGE_STATE_SUCCESS);
			List<String> resultNameList = chatMessage.getNowPahseResultName();
			if (resultNameList != null && resultNameList.size() > 0) {
				String resultNames = resultNameList.toString();
				resultNames = resultNames.substring(1, resultNames.length() - 1);
				chatMessage.setNowPhaseResultNames(resultNames);
			}
			if (!StringUtil.isEmpty(chatMessage.getRate())) {
				chatMessage.setPhaseResultRate(chatMessage.getRate());
			}

			if (SPManager.getInstance().get(SPConstants.USER_ID, "").equals(chatMessage.getFromUserId())) {
				isSendFromSelf = true;
			}

			insertOrUpdate(chatMessage);

			Intent intent = new Intent(MessageConstants.ACTION_MESSAGE_RECEIVE);
			intent.putExtra(MessageConstants.MESSAGE, chatMessage);
			context.sendBroadcast(intent);
		}

		// 如果是自己发的消息不要提示
		if (isSendFromSelf) {
			return;
		}
		// 提示至少间隔3秒才响铃一次
		String currentTime = TimeUtil.getCurrentTime();
		String msgRingTime = SPManager.getInstance().get("msgRingTime", "");
		if (!(StringUtil.isEmpty(msgRingTime)
				|| TimeUtil.calculateDiffTime(msgRingTime, currentTime, TimeUtil.UNIT_SECOND) >= 3)) {
			return;
		}
		SPManager.getInstance().put("msgRingTime", currentTime);

		CommonUtil.playVibrator(context);
		CommonUtil.playNotification(context);
	}

	public List<ChatMessage> getChatMessageList(String auctionId){
		return queryList(true, null, "auctionId='" + auctionId + "'", null, null, null, "sendTime", null);
	}

	public ChatMessage getChatMessageById(String messageId){
		return query(null, "messageId='" + messageId + "'", null);
	}

	public List<ChatMessage> getChatMessageListWithSelection(String selection){
		return queryList(null, selection, null);
	}

	public void setUnsendMessageFail(){
		ContentValues contentValues = new ContentValues();
		contentValues.put("messageState", ChatMessage.MESSAGE_STATE_FAILURE);
		String selection = "messageState='" + ChatMessage.MESSAGE_STATE_SENDING + "'";
		update(contentValues, selection, null);
	}

}
